<?php
/**
 * 订单管理
 *
 *
 */
class order extends main
{
    function __construct()
    {
        parent::__construct();
        $this->loadModel('g5_order');
    }

    function index()
    {
        $page 	  = isset($_GET[2]) ? max(1, $_GET[2]) : 1;
        $pageSize = 20;
        $search = array(
            'userName'     => isset($_POST['userName']) ? $_POST['userName'] : '',
            'userPhone'    => isset($_POST['userPhone']) ? $_POST['userPhone'] : '',
        );

        $string = isset($_GET[2]) ? $_GET[2] : '';
        if($string != ''){
            $search = unserialize(base64_decode($string));
        }

        $where    = array();

        if(!empty($search['userName'])) {
            $where['userName'] = $search['userName'];
        }
        if(!empty($search['userPhone'])) {
            $where['userPhone'] = $search['userPhone'];
        }

        $result   = $this->g5_order->limit($pageSize,($page-1)*$pageSize)->by('id')->getAll('*', $where);
        $total    = $this->g5_order->getCount('*', $where);

        $this->page($page, $total, NULL, $pageSize);
        $this->search       = $search;
        $this->result       = $result;
    }

    function add()
    {
        if($_POST['button']) {
            $version = $_POST['version'];
            $tag     = $_POST['tag'];
            $branch  = $_POST['branch'];


            $data = array(
                'version'   => $version,
                'branch'    => $branch,
                'tag'       => $tag,
                'status'    => self::STATUS_ONE,
                'addTime'   => time(),
                'addUser'   => $_SESSION['adminmanager']['user_true_name']
            );

            $this->g5_iiapple_pack->insert($data);
            $this->report('添加任务成功', BASEURI.'ipack/index');
        }
    }


    function info()
    {
        $id = isset($_GET[2]) ? max(1, $_GET[2]) : '';


        if(!empty($id)) {
            $data = $this->g5_order->getOne('*', array('id' => $id));
        }
        $this->data       = $data;
        $this->formAction = BASEURI. 'order/duAdd';
        $this->display('order.info');
    }

    function duAdd()
    {
        if($_POST['button']) {
            $userName       = $_POST['userName'];
            $userPhone      = $_POST['userPhone'];
            $userAddress    = $_POST['userAddress'];
            $buyNumber      = $_POST['buyNumber'];
            $buyPrice       = $_POST['buyPrice'];
            $buyType        = $_POST['buyType'];
            $expressNumber  = $_POST['expressNumber'];
            $remarks        = $_POST['remarks'];


            $datas = array(
                'userName'      => $userName,
                'userPhone'     => $userPhone,
                'userAddress'   => $userAddress,
                'buyNumber'     => $buyNumber,
                'buyPrice'      => $buyPrice,
                'buyType'       => $buyType,
                'expressNumber' => $expressNumber,
                'remarks'       => $remarks
            );

            if($buyType == 1) {
                $agentPrice = 43;
            } else {
                $agentPrice = 57;
            }

            if(empty($_POST['id'])) {
                $datas['addTime'] = time();
                $datas['updateTime'] = time();
                $datas['agentPrice'] = $agentPrice;
                $this->g5_order->insert($datas);
                $this->report('添加任务成功', BASEURI.'order/index');
            } else {
                $datas['updateTime'] = time();
                $this->g5_order->update($datas, array('id' => $_POST['id']));
                $this->report('修改成功', BASEURI.'order/index');
            }
        }
    }
}